﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using DrivingCRM.Controller;
using DrivingCRM.Model;

namespace DrivingCRM.Views.Private.CRM
{
    public partial class EventSignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                this.RDP_Event.SelectedDate = DateTime.Now;
                this.RDP_EventEnd.SelectedDate = DateTime.Now.AddDays(1);
            }
        }

        /// <summary>
        /// Méthode déclenchée au changement de date du champ de fin de créneau
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RDP_EventEnd_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {

            if (e.NewDate.HasValue && e.NewDate.Value < this.RDP_Event.SelectedDate)
                this.RDP_Event.SelectedDate = e.NewDate.Value.AddDays(-1);
            this.DDL_Events.DataBind();
        }

        /// <summary>
        /// Méthode déclenchée au changement de date du champ de début de créneau
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RDP_Event_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (e.NewDate.HasValue && e.NewDate.Value > this.RDP_EventEnd.SelectedDate)
                this.RDP_EventEnd.SelectedDate = e.NewDate.Value.AddDays(1);
            this.DDL_Events.DataBind();
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton d'ajout d'une inscription
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_EventSI_Click(object sender, EventArgs e)
        {
            /// recherche de l'utilisateur couramment connecté
            MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            Guid mon_id = ((Guid)u.ProviderUserKey); // ID du User connecté

            string ret = EntityManager.AddEventInscription(int.Parse(this.DDL_Events.SelectedValue), DateTime.Now, mon_id);
            
            this.L_ErrorMessage.Text = ret;
        
        }
    }
    
}